package com.dup.jxlh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dup.jxlh.admin.entities.auth.AuthInfo;
import com.dup.jxlh.admin.entities.model.Admin;
import com.dup.jxlh.admin.entities.model.Company;
import com.dup.jxlh.admin.entities.model.Department;
import com.dup.jxlh.admin.entities.model.Role;
import com.dup.jxlh.admin.entities.vo.CompanyVO;
import com.dup.jxlh.admin.entities.vo.RoleVO;
import com.dup.jxlh.admin.mapper.CompanyMapper;
import com.dup.jxlh.admin.service.*;
import com.dup.jxlh.api.entities.PaginationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;

@Service("companyServiceImpl")
public class CompanyServiceImpl implements CompanyService {
    @Resource
    private CompanyMapper companyMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private OwnValidatorService ownValidatorService;

    @Override
    public Page<CompanyVO> getSonCompanyList(BigInteger companyId, PaginationData paginationData) {
        Page<CompanyVO> companyVOPage = new Page<>(paginationData.getCurrent(), paginationData.getSize());
        companyVOPage.setOptimizeCountSql(false);

        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", companyId);
        ownValidatorService.setQueryWrapper(queryWrapper, paginationData);

        if (!paginationData.getSearch().isEmpty()) {
            queryWrapper.like("name", paginationData.getSearch());
        }

        ArrayList<CompanyVO> companyVOList = companyMapper.getSonCompanyList(companyVOPage, queryWrapper);
        if (companyVOList != null) {
            companyVOPage.setRecords(companyVOList);
        }

        return companyVOPage;
    }

    @Override
    @Transactional
    public Boolean addSonCompany(Company company, Admin admin) {
        //添加子公司
        company.setAdminId(BigInteger.valueOf(0));
        companyMapper.insert(company);

        BigInteger companyId = company.getId();

        //添加部门
        Department department = new Department();
        department.setCompanyId(companyId);
        department.setName("管理部门");
        departmentService.addDepartment(department);

        //添加角色
        Role role = new Role();
        role.setCompanyId(companyId);
        role.setName("管理员");
        roleService.addRole(role);
        //分配权限
        RoleVO roleVO = new RoleVO();
        roleVO.setId(role.getId());
        BigInteger[] permIds = {BigInteger.valueOf(1)};
        roleVO.setPermIds(permIds);
        roleService.updateRolePerm(roleVO);

        //添加管理员
        admin.setCompanyId(companyId);
        admin.setDepartmentId(department.getId());
        admin.setRoleId(role.getId());
        admin.setLevel(1);
        admin.setEnable(true);
        adminService.addAdminInfo(admin);

        company.setAdminId(admin.getId());
        companyMapper.updateById(company);
        return true;
    }

    @Override
    @Transactional
    @CacheEvict(value = "company", key = "#company.id")
    public Boolean editSonCompany(Company company, Admin admin) {
        companyMapper.updateById(company);
        adminService.updateAdminInfo(admin);
        return true;
    }

    @Override
    @Transactional
    public Boolean resetSonCompanyAdmin(BigInteger adminId) {
        AuthInfo authInfo = adminService.getAuthInfo(adminId);

        adminService.updateAdminPassword(adminId, "$2a$10$mQtfljbyADgqRJNJep9ZvecUQrTqadEMgOLpQ8ncL.HG5Qzl.ZvJK");

        BigInteger[] rolePermIds = roleService.getRolePermIds(authInfo.getRoleId());
        int check = Arrays.binarySearch(rolePermIds, BigInteger.valueOf(1));
        if (check < 0) {
            RoleVO roleVO = new RoleVO();
            roleVO.setId(authInfo.getRoleId());
            BigInteger[] bigInteger = new BigInteger[rolePermIds.length + 1];
            bigInteger[0] = BigInteger.valueOf(1);
            for (int i = 0; i < rolePermIds.length; i++) {
                bigInteger[i + 1] = rolePermIds[i];
            }
            roleVO.setPermIds(bigInteger);
            roleService.updateRolePerm(roleVO);
        }

        Admin admin = new Admin();
        admin.setId(adminId);
        admin.setPhone(authInfo.getPhone());
        admin.setLevel(1);
        adminService.updateAdminInfo(admin);

        return true;
    }

    @Override
    @Cacheable(value = "company", key = "#id")
    public Company getCompany(BigInteger id) {
        return companyMapper.selectById(id);
    }
}
